<?php
namespace Application\Api\Controller;

class WorkList extends Base
{

    private function getService()
    {
        return $this->get(SERVICE_WORKLIST);
    }

    public function indexAction()
    {
        $data = $this->getRequestData();
        $send = $this->getService()->getIndex($data['page'], $data['limit']);
        $this->sendSuccess($send);
    }

    private function setStatus($data)
    {
        if(isset($data['dry_status']) && $data['dry_status'] == 'on'){
            $data['dry_status'] = 1;
        }
        else{
            $data['dry_status'] = 0;
        }
        return $data;
    }

    public function newAction()
    {
        $data = $this->getRequestData();
        $device = DEVICE_WEIXIN_SP;
        if(setAndNotEmpty($data, 'device')){
            $device = $data['device'];
        }
        $serviceUser = $this->get(SERVICE_USER);
        $userId = $serviceUser->checkToken($data, $device);
        if(!$userId){
            $this->sendFail('TOKEN_ERROR', '', __LINE__);
            return false;
        }
        $service = $this->getService();
        unset($data['id'],$data['device'],$data['token'],$data['timestamp'],$data['rand'],$data['sign'],$data['imageList']);
        $data = addDateTime($data, THE_TIME);
        $data['dry_user'] = $userId;
        $data['dry_data_sort'] = 0;
        $data['dry_sort'] = isset($data['dry_sort']) ? $data['dry_sort'] : 0;
        $data['dry_status'] = isset($data['dry_status']) ? $data['dry_status'] : 1;
        $id = $service->add($data);
        $send = [
            'id' => $id
        ];
        $this->sendSuccess($send);
    }

    public function editAction()
    {
        $data = $this->getRequestData();
        $device = DEVICE_WEIXIN_SP;
        if(setAndNotEmpty($data, 'device')){
            $device = $data['device'];
        }
        $serviceUser = $this->get(SERVICE_USER);
        $userId = $serviceUser->checkToken($data, $device);
        if(!$userId){
            $this->sendFail('TOKEN_ERROR', '', __LINE__);
            return false;
        }
        $service = $this->getService();
        $id = $data['id'];
        unset($data['id'],$data['device'],$data['token'],$data['timestamp'],$data['rand'],$data['sign'],$data['imageList']);
        $data = addDateTime($data, THE_TIME);
        $rows = $service->update($data, $id);
        $send = [
            'rows' => $rows
        ];
        $this->sendSuccess($send);
    }

    public function deleteAction(){
        $data = $this->getRequestData();
        $device = DEVICE_WEIXIN_SP;
        if(setAndNotEmpty($data, 'device')){
            $device = $data['device'];
        }
        $serviceUser = $this->get(SERVICE_USER);
        $userId = $serviceUser->checkToken($data, $device);
        if(!$userId){
            $this->sendFail('TOKEN_ERROR', '', __LINE__);
            return false;
        }
        $service = $this->getService();
        $id = $data['id'];
        $rows = $service->delete($id);
        $send = [
            'rows' => $rows
        ];
        $this->sendSuccess($send);
    }

    /*
        后台工作记录
    */
    public function getListAction()
    {
        $data = $this->getRequestData();
        $device = DEVICE_WEIXIN_SP;
        if(setAndNotEmpty($data, 'device')){
            $device = $data['device'];
        }
        $serviceUser = $this->get(SERVICE_USER);
        $userId = $serviceUser->checkToken($data, $device);
        if(!$userId){
            $this->sendFail('TOKEN_ERROR', '', __LINE__);
            return false;
        }
        $page = (int)tryGet($data, 'page', 1);
        $limit = (int)tryGet($data, 'limit', 1000);
        $temp = $this->getService()->getList($userId, $page, $limit);
        $list = $this->get(SERVICE_FILE)->getFileData($temp['list'], ['dry_image' => 2]);
        $data = [
            'oss_domain' => getOssDomain().'/',
            'list' => $list,
            'count' => $temp['count']
        ];
        $this->sendSuccess($data);
    }

    public function getListByFieldsAction()
    {
        $data = $this->getRequestData();
        $device = DEVICE_WEIXIN_SP;
        if(setAndNotEmpty($data, 'device')){
            $device = $data['device'];
        }
        $serviceUser = $this->get(SERVICE_USER);
        $userId = $serviceUser->checkToken($data, $device);
        if(!$userId){
            $this->sendFail('TOKEN_ERROR', '', __LINE__);
            return false;
        }
        $data['dry_user'] = $userId;
        $page = (int)tryGet($data, 'page', 1);
        $limit = (int)tryGet($data, 'limit', 1000);
        unset($data['device'],$data['token'],$data['timestamp'],$data['rand'],$data['sign'],$data['page']);
        $temp = $this->getService()->getListByFields($data,$page,$limit);
        $tempList = $this->get(SERVICE_OSSFILE)->getFileData($temp['list'], ['dry_image' => 2]);
        if($data['dry_worker']){
            for($i = 0 ;$i < count($tempList);$i++){
                $arr = explode(',',$tempList[$i]->dry_worker);
                if(!in_array($data['dry_worker'],$arr)){
                    unset($tempList[$i]);
                    $temp['count']--;
                }
            }
        }
        $list = [];
        if($data['id']) {
            $list = $tempList;
        }
        else {
            usort($tempList,function($a,$b){
                if ($a->dry_add_date == $b->dry_add_date) return 0;
                return ($a->dry_add_date < $b->dry_add_date) ? 1 : -1;
            });
            $date = [];
            foreach($tempList as $v){
                array_push($date,substr($v->dry_add_date,0,7));
            }
            $date = array_unique($date);
            foreach ($date as $v) {
                $list[$v] = [];
                foreach ($tempList as $t){
                    $str = substr($t->dry_add_date,0,7);
                    if($str == $v){
                        array_push($list[$v],$t);
                    }
                }
            }
        }
        $data = [
            'oss_domain' => getOssDomain().'/',
            'list' => $list,
            'count' => $temp['count']
        ];
        $this->sendSuccess($data);
    }

    /*
        /Api/WorkList/getUploadData
    */
    public function getUploadDataAction()
    {
        $data = $this->get(SERVICE_OSS)->getUploadData('spwork');
        $this->sendSuccess($data);
    }

}